<!--
 * @Description:
 * @Author: linjz
 * @Date: 2021-06-25 10:25:00
 * @LastEditors: linjz
 * @LastEditTime: 2021-06-28 17:20:25
-->
<template>
  <!-- 产品栏目管理 -->
  <div>
    <!-- 添加按钮 -->
    <el-button type="primary" @click="toAdd">添加</el-button>

    <!-- 表格区域 -->
    <div>
      <!-- {{ tableData }} -->
      <el-table :data="tableData">
        <el-table-column type="index" label="序号" align="center" />
        <el-table-column prop="id" label="栏目编号" align="center" />
        <el-table-column prop="name" label="名称" align="center" />
        <el-table-column label="图标" align="center" width="50px">
          <template slot-scope="scope">
            <el-image :src="scope.row.icon" />
          </template>
        </el-table-column>
        <el-table-column prop="num" label="排序号" align="center" />
        <el-table-column prop="address" label="操作" align="center">
          <template slot-scope="scope">
            <el-button type="text" @click="toEdit(scope.row)">编辑</el-button>
            <el-button
              type="text"
              class="style_color"
              @click="toDelete(scope.row.id)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div class="page">
      <el-pagination
        :current-page="params.page"
        :page-sizes="[2, 5, 10, 15, 20]"
        :page-size="params.pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
      />
    </div>

    <!-- 模态框 -->
    <div>
      <el-dialog
        :title="title"
        :visible.sync="visible"
        width="40%"
        @close="toClose"
      >
        <!-- {{ ruleForm }} -->
        <el-form
          ref="ruleForm"
          :model="ruleForm"
          :rules="rules"
          label-width="80px"
          class="demo-ruleForm"
        >
          <el-form-item label="栏目名称" prop="name">
            <el-input v-model="ruleForm.name" />
          </el-form-item>
          <el-form-item label="序号" prop="num">
            <el-input v-model.number="ruleForm.num" />
          </el-form-item>
          <el-form-item label="图标" prop="icon">
            <el-upload
              class="avatar-uploader"
              :action="uploadFileURL"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img v-if="imageUrl" :src="imageUrl" class="avatar" />
              <i v-else class="el-icon-plus avatar-uploader-icon" />
            </el-upload>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="visible = false">取 消</el-button>
          <el-button type="primary" @click="toSave">确 定</el-button>
        </span>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import { pageQuery, saveOrUpdate, deleteById } from "@/api/proCategory";
import { uploadFileURL, showFileURL } from "../../../utils/config";
export default {
  data() {
    return {
      // 表格展示的数据
      tableData: {},
      params: {
        page: 1,
        pageSize: 10,
      },
      // 总条数
      total: 0,
      // 模态框数据
      ruleForm: {},
      // 校验规则
      rules: {
        name: [{ required: true, message: "请输入活动名称", trigger: "blur" }],
        num: [{ type: "number", message: "序号必须为数字值", trigger: "blur" }],
        icon: [{ required: true, message: "请选择图标", trigger: "change" }],
      },
      // 显示模态框
      visible: false,
      // 模态框标题
      title: "",
      // 上传图片地址
      uploadFileURL,
      showFileURL,
      // 上传的图片的路径
      imageUrl: "",
    };
  },
  computed: {},
  watch: {
    // 深度监听分页参数
    params: {
      handler() {
        this.queryCategoryData();
      },
      deep: true,
    },
  },
  created() {
    this.queryCategoryData();
  },
  mounted() {},
  methods: {
    // 获取栏目分类数据
    async queryCategoryData() {
      const res = await pageQuery(this.params);
      this.tableData = res.data.list;
      this.total = res.data.total;
    },
    // 每页显示条数发生变化时
    handleSizeChange(val) {
      this.params = {
        page: 1,
        pageSize: val,
      };
    },
    // 页数发生变化时
    handleCurrentChange(val) {
      this.params.page = val;
    },
    // 添加按钮点击处理事件
    toAdd() {
      this.title = "添加栏目信息";
      this.ruleForm = {};
      this.imageUrl = "";
      this.visible = true;
    },
    // 重置表单验证
    toClose() {
      this.$refs.ruleForm.resetFields();
    },
    // 图片上传成功后的回调函数
    handleAvatarSuccess(res, file) {
      this.imageUrl = URL.createObjectURL(file.raw);
      this.ruleForm.icon = showFileURL + res.data.id;
    },
    // 图片上传之前的回调函数
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    // 确定按钮点击处理事件
    toSave() {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          const res = await saveOrUpdate(this.ruleForm);
          // console.log(res);
          if (res.status === 200) {
            this.$notify({
              title: "成功",
              message: res.message,
              type: "success",
            });
            this.queryCategoryData();
            this.toClose();
            this.visible = false;
          }
        } else {
          return false;
        }
      });
    },
    // 编辑按钮点击处理事件
    toEdit(row) {
      this.title = "编辑栏目信息";
      this.ruleForm = { ...row };
      this.imageUrl = row.icon;
      this.visible = true;
    },
    // 删除按钮点击处理事件
    async toDelete(id) {
      const res = await deleteById({ id });
      // console.log(res);
      if (res.status === 200) {
        this.$notify({
          title: "成功",
          message: res.message,
          type: "success",
        });
      }
      this.queryCategoryData();
    },
  },
};
</script>
<style scoped>
.style_color {
  color: #e65d6e;
}
.page {
  float: right;
  margin-top: 10px;
}
</style>
